package com.heima.bos.dao.impl;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.Root;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.heima.bos.dao.DaoInter;
import com.heima.bos.utils.PageBean;
/**
 * 抽取的Dao
 * @author Administrator
 *
 * @param <T>
 */
@SuppressWarnings("all")
public class BaseDaoImpl<T> extends HibernateDaoSupport implements DaoInter<T>{
	@Resource
	public void setSF(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	//获取泛型
	private Class clazz;
	public BaseDaoImpl(){
		ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
		Type[] types = type.getActualTypeArguments();
		clazz = (Class) types[0];
	}
	//查询所有
	public List<T> findAll() {
		String hql = "from "+ clazz.getSimpleName();
		List<T> list = this.getHibernateTemplate().find(hql);
		return list;
	}
	//增加对象
	public void save(T entity) {
		this.getHibernateTemplate().save(entity);
	}
	//更新对象
	public void update(T entity) {
		this.getHibernateTemplate().update(entity);
	}
	//删除
	public void delete(T entity) {
		this.getHibernateTemplate().delete(entity);
	}
	//根据Id查询对象
	public T findById(Serializable id) {
		return (T) this.getHibernateTemplate().get(clazz, id);
	}
	//离线查询
	public List<T> findByCriteria(DetachedCriteria criteria) {
		return this.getHibernateTemplate().findByCriteria(criteria);
	}

	public List<T> findByNameQuery(String queryName, Object... orgs) {
		return this.getHibernateTemplate().findByNamedQuery(queryName, orgs);
	}
	
	public void executeNameQuery(String queryName, Object... orgs) {
		Session session = this.getSession();
		Query query = session.getNamedQuery(queryName);
		if(orgs != null && orgs.length>0){
			int i=0;
			for (Object object : orgs) {
				query.setParameter(i++, object);
			}
		}
		query.executeUpdate();
	}
	//分页
	public void pageList(PageBean<T> pageBean) {
		Integer currentPage = pageBean.getPage();
		Integer pageSize = pageBean.getPageSize();
		
		DetachedCriteria detachedCriteria = pageBean.getDetachedCriteria();
		
		detachedCriteria.setProjection(Projections.rowCount());
		List<Long> list = this.getHibernateTemplate().findByCriteria(detachedCriteria);
		Long total = list.get(0);
		
		detachedCriteria.setProjection(null);
		detachedCriteria.setResultTransformer(DetachedCriteria.ROOT_ENTITY);
		
		int firstResult = (currentPage-1)*pageSize;
		int maxResults = pageSize;
		List list1 = this.getHibernateTemplate().findByCriteria(detachedCriteria, firstResult, maxResults);
		pageBean.setTotal(total.intValue());
		pageBean.setRows(list1);
	}

}
